<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分类编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryCode">
              <j-category-select v-model="model.categoryCode" pcode="A01" back="code" placeholder="请选择分类编号"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectCode">
              <a-input v-model="model.projectCode" placeholder="请输入项目编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input v-model="model.projectName" placeholder="请输入项目名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectHeadId">
              <j-select-user-by-dep v-model="model.projectHeadId" @back="selProjectHeadChange" :multi="false" :backUser="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="负责人所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectHeadId">
              <j-select-depart placeholder="请选择负责人所属部门" v-model="model.projectHeadDeptId" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectStart">
              <j-date placeholder="请选择项目开始时间" v-model="model.projectStart" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectEnd">
              <j-date placeholder="请选择项目结束时间" v-model="model.projectEnd" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectDesc">
              <a-textarea v-model="model.projectDesc" rows="4" placeholder="请输入项目简介" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectState">
              <j-dict-select-tag type="list" v-model="model.projectState" dictCode="project_state" placeholder="请选择项目状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "MtProjectModal",
    components: { 
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           categoryCode: [
              { required: true, message: '请选择分类!'},
           ],
           projectCode: [
              { required: true, message: '请输入项目编号!'},
              { validator: (rule, value, callback) => validateDuplicateValue('mt_project', 'project_code', value, this.model.id, callback)},
           ],
           projectName: [
              { required: true, message: '请输入项目名称!'},
           ],
        },
        url: {
          add: "/mytest/mtProject/add",
          edit: "/mytest/mtProject/edit",
        }
     
      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      selProjectHeadChange(userInfo){
        this.model.projectHeadDeptId = userInfo[0].departIds;
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      handleCategoryChange(value,backObj){
        this.model = Object.assign(backObj,this.model);
      }

      
    }
  }
</script>